package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import algri.AnaData;
import db.Station_DB;

/**
 * Servlet implementation class Req_getStationWarningLevel
 */
@WebServlet("/Req_getStationWarningLevel")
public class Req_getStationWarningLevel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Req_getStationWarningLevel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "Get,Post");
		Station_DB stations_statusDB=new Station_DB();
		AnaData analyzedata=new AnaData();
		//从前端获取限制条件数据
		String spot_name=request.getParameter("spot_name");
		String refresh_time=request.getParameter("refresh_time");
		//打印测试
		System.out.println("矿区地点信息:"+spot_name+"刷新时间:"+refresh_time);
		//接受数据库数据list
		List<HashMap<String, String>> db_list=new ArrayList<HashMap<String,String>>();
		//返回前端HashMap定义
		HashMap<String, String> restMap=new HashMap<String, String>();
		List<HashMap<String, String>> restList=new ArrayList<HashMap<String,String>>();
		//从数据库层请求数据
		db_list=stations_statusDB.returnResult(spot_name, refresh_time);
		//将数据集传至分析方法中进行分析，并返回进行数据填充
		restList=analyzedata.ana_station_status(db_list);
		Gson gson=new Gson();
		String staion_status_restapiString=gson.toJson(restList);
		System.out.println("工况预警等级img接口:"+staion_status_restapiString);
		out.write(staion_status_restapiString);
		out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
